package com.longxi.tianjintianbao.ui.adapter.multitype.contact;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.longxi.tianjintianbao.R;
import com.longxi.tianjintianbao.listener.SearchListener;
import com.longxi.tianjintianbao.listener.SelectContactDetailListener;
import com.longxi.tianjintianbao.model.Search;
import com.longxi.tianjintianbao.model.contact.ContactDepartment;
import com.longxi.tianjintianbao.model.contact.ContactDetail;
import com.longxi.tianjintianbao.ui.activity.contact.ContactSearchActivity;
import com.longxi.tianjintianbao.ui.adapter.multitype.SearchViewBinder;
import com.longxi.tianjintianbao.ui.presenter.contact.ContactSearchAtPresenter;

import java.io.Serializable;

import me.drakeet.multitype.ItemViewBinder;
import me.drakeet.multitype.Items;
import me.drakeet.multitype.MultiTypeAdapter;

/**
 * Created by cxw on 2018/2/20.
 */
/**
 * Created by cxw on 2018/2/20.
 */
public class ContactDeptnamentViewBinder extends ItemViewBinder<ContactDepartment, ContactDeptnamentViewBinder.ViewHolder> {

    private SelectContactDetailListener selectContactDetailListener;

    public ContactDeptnamentViewBinder(SelectContactDetailListener selectContactDetailListener) {
        this.selectContactDetailListener = selectContactDetailListener;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View root = inflater.inflate(R.layout.item_contact_deptment_list, parent, false);
        return new ViewHolder(root);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull ContactDepartment contactDepartment) {
        holder.mTvContactName.setText(contactDepartment.getDeptname());
        holder.mViewLine.setVisibility(View.GONE);
        holder.mIvContactHead.setImageResource(R.mipmap.ic_no_head);

        if(contactDepartment.isOpen()){
            holder.mRvContent.setVisibility(View.VISIBLE);
            holder.mIvArrow.setImageResource(R.mipmap.ic_up_arrow);
        }else{
            holder.mRvContent.setVisibility(View.GONE);
            holder.mIvArrow.setImageResource(R.mipmap.ic_down_arrow);
        }

        holder.mRlBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(contactDepartment.isOpen()){
                    holder.mRvContent.setVisibility(View.GONE);
                    contactDepartment.setOpen(false);
                    holder.mIvArrow.setImageResource(R.mipmap.ic_down_arrow);
                }else{
                    holder.mRvContent.setVisibility(View.VISIBLE);
                    contactDepartment.setOpen(true);
                    holder.mIvArrow.setImageResource(R.mipmap.ic_up_arrow);
                }
            }
        });

        holder.items.clear();
        holder.items.addAll(contactDepartment.getDetailed());
        holder.adapter.notifyDataSetChanged();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        View mViewLine;
        LinearLayout mRlBtn;
        ImageView mIvContactHead;
        TextView mTvContactName;
        ImageView mIvArrow;
        RecyclerView mRvContent;

        MultiTypeAdapter adapter;
        Items items;
        ViewHolder(View itemView) {
            super(itemView);
            mViewLine = itemView.findViewById(R.id.viewLine);
            mRlBtn = itemView.findViewById(R.id.rlBtn);
            mIvContactHead = itemView.findViewById(R.id.ivContactHead);
            mTvContactName = itemView.findViewById(R.id.tvContactName);
            mIvArrow = itemView.findViewById(R.id.ivArrow);
            mRvContent = itemView.findViewById(R.id.rvContent);

            adapter = new MultiTypeAdapter();
            adapter.register(ContactDetail.class, new ContactListViewBinder(selectContactDetailListener));
            mRvContent.setAdapter(adapter);
            items = new Items();
            adapter.setItems(items);
        }
    }
}
